<%@page import="org.porridge.util.SVNManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>版本更新工具</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resource/jquery-1.7.2.min.js" ></script>
<script language="javascript" type="text/javascript" src="${pageContext.request.contextPath}/resource/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
function setCookie(name,value,expireday){
	var exp = new Date();
	exp.setTime(exp.getTime()+expireday*24*60*60*1000); //设置cookie的期限
	document.cookie =trim(name)+"="+escape(value)+"; expires"+"="+exp.toGMTString();//创建cookie
}
function getCookie(name) {
	var cookieStr = document.cookie;
	if(cookieStr.length > 0) {
		var cookieArr = cookieStr.split(";"); //将cookie信息转换成数组
		name=trim(name);
		for (var i=0; i<cookieArr.length; i++) {
			var cookieVal = cookieArr[i].split("="); //将每一组cookie(cookie名和值)也转换成数组
			if(trim(cookieVal[0])==trim(name)){
				return unescape(cookieVal[1]); //返回需要提取的cookie值
			}
		}
	}
}
function trim(str){//正则掉二边的空格
	return str.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, '');
}
function convertObject(strData){//转换对象
	return (new Function("return " + strData ))();
}
function is_number(e){
    var char_code = e.charCode ? e.charCode : e.keyCode;
    if(char_code==8 || char_code==9 || char_code==45){//当输入. 或者删除时不阻止
    	return true;
    }if(char_code<48 || char_code >57){
        return false;  
    }else{  
        return true;      
    }
}
var svn={
		svnurl:null,
		login:function(){//登录svn服务器处理
			var tip="";
			$("#myform :input").each(function(k,v){
				if($(this).val()=="" && $(this).attr("name")!="address"){
					tip+=$(this).attr("name")+" 不能为空<br/>";
				}
			});
			if(tip!="")
				$("#prompt").html(tip).addClass("red");
			if(tip==""){
				$("#prompt").removeClass("red");
				$("#myform").hide(0);
				$.post($("#myform").attr("action"),$("#myform").serialize(),function(data){
					data=convertObject(data);//数据过多可能会造成格式乱掉，所以以txt接收。然后再转换一下成Json
					if(data.resoult=="true"){
						$("#prompt").html("登录成功!正在准备数据...");
						svn.toggle(1);
						document.getElementById("log_list").innerHTML="";
						svn.filelist($("#address").val(),"svn_lists");
						svn.svnurl=$("#myform input[name=svn]").val();
						setCookie("svn_"+svn.svnurl,$("#myform").serialize(),30);//写入cookie
						$("#title_toggle .findli").removeClass("findli");
						$("#title_toggle li:eq(0)").addClass("findli");
					}else{
						$("#myform").show(0);
						$("#prompt").html("登录失败，正在准备错误数据...").addClass("red");
						var ht="";
						$.each(data.errors,function(k,v){
							ht+=v.key+"，"+v.value+"<br/>";
						});
						$("#prompt").html(ht);
					}
				},"text");
			}
		},titletog:function(){//注册切换事件
			$("#title_toggle li").click(function(){
				if(this.id!=""){
					$("#title_toggle .findli").removeClass("findli");
					$(this).addClass("findli");
					if(this.id=="li_data"){//库浏览
						svn.toggle(1);
						svn.filelist($("#address").val(),"svn_lists");
					}else{//日志
						svn.toggle(2);
					}
				}
			});
		},toggle:function(i){//0显示登录   1显示库   2 显示日志加载
			if(i==0){
				$("#title_toggle,#file_div,#log_div").hide(0);
				$("#login_div,#myform").show(0);
			}else if(i==1){
				$("#title_toggle,#file_div").show(0);
				$("#login_div,#myform,#log_div").hide(0);
			}else if(i==2){
				$("#title_toggle,#log_div").show(0);
				$("#login_div,#myform,#file_div").hide(0);
				svn.setSeach_log_form();
			}
		},filelist:function(path,id){//查询目录
			if(id=="svn_lists")//如果是第一次加载勿必清空
				document.getElementById(id).innerHTML="";
			document.getElementById("getfile_lable").innerHTML=path;
			$.post("${pageContext.request.contextPath}/findsvnfile",{"path":path},function(data){
				data=convertObject(data);
				var ul=document.createElement("ul");
				$.each(data.files,function(k,v){
					var liid="li_"+Math.floor(Math.random()*(987654321+1));
					var li=document.createElement("li");
					li.id=liid;
					var div=document.createElement("div");
					div.innerHTML=v.name+"<"+v.svnversion+">";
					if(v.isfile=="false"){//必须是文件夹才绑定单击事件
						div.onclick=function(){
							if(document.getElementById(liid).children.length!=1){//证明已加载过数据了。只需要控制显示隐藏
								if(this.className=="openfolder"){
									document.getElementById(liid).children[1].style.display="none";
									document.getElementById(liid).children[0].className="folder";
								}else{
									document.getElementById(liid).children[1].style.display="block";
									document.getElementById(liid).children[0].className="openfolder";
								}
							}else{
								svn.filelist(path+"/"+v.name,liid);
								document.getElementById(liid).children[0].className="openfolder";
							}
						};
					}
					div.className=v.isfile=="true"?"file":"folder";
					li.appendChild(div);
					ul.appendChild(li);
				});
				document.getElementById(id).appendChild(ul);
			},"text");
		},fundsvnlog:function(){//查询日志记录
			document.getElementById("log_list").innerHTML="";
			$.post("${pageContext.request.contextPath}/svnfindlog",$("#seach_log_form").serialize(),function(data){
				data=convertObject(data);
				var ul=document.createElement("ul");
				$.each(data,function(key,value){
					var liid="log_"+Math.floor(Math.random()*(987654321+1));
					var li=document.createElement("li");
					li.id=liid;
					var div=document.createElement("div");
					div.innerHTML="版本号:"+value.svnversion+" 提交人:"+value.submituser+"  提交时间:"+value.submittime;
					div.onclick=function(){
							if(this.className=="openfolder"){
								document.getElementById(liid).children[1].style.display="none";
								document.getElementById(liid).children[2].style.display="none";
								document.getElementById(liid).children[0].className="folder";
							}else{
								document.getElementById(liid).children[1].style.display="block";
								document.getElementById(liid).children[2].style.display="block";
								document.getElementById(liid).children[0].className="openfolder";
							}
					};
					div.className="folder";
					li.appendChild(div);
					
					var tdiv=document.createElement("div");
					tdiv.innerHTML=value.remark;
					tdiv.className="remark";
					tdiv.style.display="none";
					li.appendChild(tdiv);
					
					var tul=document.createElement("ul");
					$.each(value.files,function(k,v){
						var tli=document.createElement("li");
						//A 指添加数据   D 指删除数据   M指修改数据
						var type="";
						if(v.type=="A")
							type="添加";
						else if(v.type=="D")
							type="删除";
						else if(v.type=="M")
							type="修改";
						tli.innerHTML=type+"-"+v.path;
						tul.appendChild(tli);
					});
					tul.style.display="none";
					li.appendChild(tul);
					ul.appendChild(li);
				});
				document.getElementById("log_list").appendChild(ul);
			},"text");
		},clearsvn:function(){
			$.post("${pageContext.request.contextPath}/clearsvn",function(){
				document.getElementById("prompt").innerHTML="已成功退出";
				svn.svnurl=null;
				setTimeout(function(){
					document.getElementById("prompt").innerHTML="";
				},5000);
				svn.toggle(0);
				svn.getcookie();
			},"text");
		},packfile:function(){
			if($("#seach_log_form input[name=localpath]").val()==""){
				alert("本地项目地址不能为空！");
				return;
			}
			if($("#seach_log_form input[name=createproject]").val()==""){
				alert("生成地址不能为空！");
				return;
			}
			var vl=$("#seach_log_form").serialize();
			$.post("${pageContext.request.contextPath}/createfile",$("#seach_log_form").serialize(),function(data){
				setCookie("pack_"+trim(svn.svnurl),vl,30);
				alert("打包成功!");
			},"html");
		},getcookie:function(){//读取是否存在登录历史信息。
			var cks=document.cookie.split(";");
			var t1=null;
			var ht="";
			for(var i=0;i<cks.length;i++){
				t1=cks[i].split("=");
				if(t1[0].indexOf("svn_")!=-1){
					ht+="<li><a href=\"javascript:svn.setLoginCookie('"+t1[0]+"')\">"+t1[0].replace("svn_","")+"</a></li>";
				}
			}
			if(ht!=""){
				$("#cookie_svn li:not(:eq(0))").remove();
				$("#cookie_svn").show(0).append(ht);
			}else{
				$("#cookie_svn").hide(0);
			}
		},setLoginCookie:function(ckid){//写入登录信息
			var ts=decodeURIComponent(getCookie(ckid)).split("&");
			for(var i=0;i<ts.length;i++){
				var tf=ts[i].split("=");
				$("#myform input[name="+tf[0]+"]").val(tf[1]);
			}
		},setSeach_log_form:function(){
			var ts=decodeURIComponent(getCookie("pack_"+svn.svnurl)).split("&");
			for(var i=0;i<ts.length;i++){
				var tf=ts[i].split("=");
				$("#seach_log_form input[name="+tf[0]+"]").val(tf[1]);
			}
		}
}
$(function(){
	$("body").ajaxStart(function(){
		$("#load_img").slideDown(100);
		$(":input:visible").attr("disabled","disabled");
	});
	$("body").ajaxStop(function(){
		$("#load_img").slideUp(100);
		$(":input[disabled]").removeAttr("disabled");
	});
	svn.getcookie();
	svn.titletog();//注册登录后的切换事件
	$("#login").click(function(){
		svn.login();
	});
	if("${sessionScope.svn}"!=""){
		svn.toggle(1);
		svn.filelist("","svn_lists");
		<%
		SVNManager svn=(SVNManager)session.getAttribute("svn");
		if(svn!=null){
			out.print("svn.svnurl='"+svn.po.getSvn()+"';");
		}
		%>
	}
});
</script>
<style type="text/css">
#cookie_svn{display:none;}
.red{color:red;}
*{margin:0px;padding:0px;font: 14px/1.5 tahoma,arial,宋体;}
ol,ul,li {list-style: none;list-style-type: none;}
.prompt{width:100%;padding:10px 0px;padding-left:155px;}
#getfile{width:100%;height:30px;border-bottom:1px #cdcdcd solid;}
#svn_lists ul,#log_list ul{margin-left:15px;}
#svn_lists li,#log_list li{padding:3px 0px 3px 5px;}
#svn_lists div,#log_list div{text-align:left;text-indent:17px;}
.folder:hover,.openfolder:hover{color:#0000CC;}
.folder{background:url("${pageContext.request.contextPath}/resource/bg.png") 0px 5px no-repeat; cursor:pointer;}
.openfolder{background:url("${pageContext.request.contextPath}/resource/bg.png") 0px -12px no-repeat; cursor:pointer;}
.file{background:url("${pageContext.request.contextPath}/resource/bg.png") 0px -29px no-repeat;}
#title_toggle{width:100%;height:30px;border-bottom:3px solid #FFB5F2;}
#title_toggle li{width:100px;height:30px;text-align:center;float:left;cursor:pointer;}
#title_toggle .findli{background:#F2F2F2;color:red;}
#log_seach div{padding:5px 0px; text-indent:10px;}
#log_seach div input{border: 1px solid #999999;height: 20px;width:142px;}
#log_seach div .longinput{width:300px;}
#log_div .labletext,#login_div .labletext{width:150px;text-align:right;float:left;padding: 0px;padding-right:5px;}
#log_seach .red{margin-left:5px;}
.remark{width:98%;border:1px #cdcdcd dashed;padding:6px;}
#login_div{width:800px;margin:0 auto;}
#login_div .onediv{width:100%;padding:5px 0px;text-align: left;}
#login_div input{width:500px;height:25px;border:1px #cdcdcd solid;}
#login_div #login{height:35px; cursor: pointer;font-size: 18px;}
#cookie_svn{width:100%;padding:10px 0px;text-indent:150px;}
#cookie_svn li{height:30px;line-height:30px;}
#log_seach .demo_test{padding-left:50px;text-indent: 0px;font-size: 12px;}
#load_img{background: none repeat scroll 0 0 #eff4fa;position:fixed;border-top: 1px solid #cdcdcd;bottom:0;height: 50px;line-height: 50px;text-align: center;width: 100%;display:none;}
.what{font-size:14px;color:#00C;cursor: help;margin-left:10px;}
</style>
</head>
<body>
<div id="load_img">系统正在请求数据中...</div>
<div id="login_div">
<div id="prompt" class="prompt"></div>
<ul id="cookie_svn">
<li>cookie历史记录</li>
</ul>
<!-- 
注册地址：
http://code.svnspot.com/zhtemp/test
测试地址：
https://m2.svnspot.com/zhtemp.test/
测试用户名：
zhtemp
测试密码：
svnspotpas123456
 -->
<form id="myform" action="${pageContext.request.contextPath}/svnlogin">
<div class="onediv"><div class="labletext">&nbsp;</div><a title="什么是版本生成条件？" class="what" target="_blank" href="help.jsp">help</a></div>
<div class="onediv"><div class="labletext">svn:</div><input type="text" name="svn" /></div>
<div class="onediv"><div class="labletext">username:</div><input type="text" name="username" /></div>
<div class="onediv"><div class="labletext">password:</div><input type="text" name="password"/></div>
<div class="onediv"><div class="labletext">address:</div><input type="text" id="address" title="初始化SVN请求地址" name="address" /></div>

<div class="onediv"><div class="labletext">&nbsp;</div><input id="login" type="button" value="login"  /></div>
</form>
</div>
<ul id="title_toggle" style="display:none;">
<li id="li_data" class="findli">库浏览</li>
<li id="li_log">日志查询</li>
<li style="float:right;"><button type="button" onclick="svn.clearsvn()">退出系统</button></li>
</ul>
<div id="file_div" style="display:none;">
<div id="getfile">
当前请求地址：<label id="getfile_lable"></label>
</div>
<div id="svn_lists"></div>
</div>
<div id="log_div" style="display:none;">
<div id="log_seach">
<form id="seach_log_form"  action="">
<div>搜索条件</div>
<div><div class="labletext" >时间范围：</div><input name="datefirst" type="text" onFocus="WdatePicker()"/> - <input name="datelast" type="text" onFocus="WdatePicker()"/></div>
<div><div class="labletext" >版本范围：</div><input name="versionsfirst" onkeypress="return is_number(event)" type="text"/> - <input name="versionslast" onkeypress="return is_number(event)" type="text"/></div>
<div><div class="labletext" >用户过滤：</div><input name="username" class="longinput" type="text"/><span class="red">多用户以;分号隔开</span></div>
<div><div class="labletext" >指定搜索目录过滤：</div><input name="seachfile" class="longinput" type="text"/><span class="red">多目录以;分号隔开</span></div>
<div>版本生成条件
<a title="什么是版本生成条件？" class="what" target="_blank" href="help.jsp">help</a>
</div>
<div><div class="labletext" >本地项目地址：</div><input name="localpath" class="longinput" type="text"/></div>
<div><div class="labletext" >生成地址：</div><input name="createproject" class="longinput" type="text"/></div>
<div><div class="labletext" >生成省略地址：</div><input name="delpath" class="longinput" type="text"/></div>
<div><div class="labletext" >项目打包：</div><input value="true" name="isjowwei" style="width:20px;" class="longinput" type="checkbox"/>
</div>
</form>
<div>
<button onclick="svn.fundsvnlog()" type="button">查询日志</button>
<button onclick="svn.packfile()" type="button">打包数据</button>
</div>
</div>
<div id="log_list">
</div>
</div>
</body>
</html>